/* concealed carry data cleaning .do file
files used: gunpolicy.dta, State_Laws.csv
files created: monthly_gun_policies, annual_state_laws
programs used: clean_cc_policy
*/
********************************************************************************

** Convert statefirearmlaws.org data to .dta file

import delimited "Raw Data/Policy Data/State_Laws.csv", encoding(ISO-8859-1) clear

keep state year lawtotal 

replace state = lower(state)

save "Raw Data/Policy Data/annual_state_laws.dta", replace

*Clean state level concealed carry policy data for merge


*Bring in data
use "Raw Data/Policy Data/gunpolicy.dta", clear

** BRADY

gen brady_wait = 0 
replace brady_wait = 1 if brady_instant == 0

** PERMITS **

gen PERMIT = 0
replace PERMIT = 1 if permit > 0
replace PERMIT = . if permit == . 

** CORRECTIONS FROM POLIQUIN

replace PERMIT = 1 if state == "Missouri"
replace PERMIT = 0 if state == "Michigan" & year >= 2013

egen sample_month = group(year month)
replace brady_wait = 1 if state == "Ohio" & sample_month>=290 & sample_month <= 347

** CONCEALED CARRY **

gen conceal_carry = 0
replace conceal_carry = 1 if concealed_carry > 0

gen unrestricted = 0
replace unrestricted = 1 if concealed_carry == 1

gen shall_issue = 0
replace shall_issue = 1 if concealed_carry == 2

gen may_issue = 0
replace may_issue = 1 if concealed_carry == 3

gen prohibited = 0
replace prohibited = 1 if concealed_carry == 4


** DEALER CHECKS **

gen DEALER_CHECK = 0
replace DEALER_CHECK = 1 if dealer_checks > 0
	
** BACKGROUND CHECKS **

gen BACKGROUND_CHECK = 0
replace BACKGROUND_CHECK = 1 if bg_check > 0
replace BACKGROUND_CHECK = 1 if dealer_check > 0
replace BACKGROUND_CHECK = . if bg_check ==.
replace DEALER_CHECK = . if dealer_checks == .

** WAITING PERIODS **

gen ANY_WAIT = 0
replace ANY_WAIT = 1 if handgun_wait > 0 | longgun_wait > 0 
replace ANY_WAIT = 1 if handgun_wait > 0 & longgun_wait > 0 
replace ANY_WAIT = 1 if PERMIT == 1 
replace ANY_WAIT = 1 if brady_wait == 1

** Collapse policies
collapse (max) brady_wait PERMIT (sum) ANY_WAIT BACKGROUND_CHECK DEALER_CHECK unrestricted shall_issue may_issue prohibited, ///
	by(fips usps state month year)

	
/* need to recode concealed carry vars such that policy that was in place 
for majority of month is treatment assigned to state */

foreach var of varlist unrestricted shall_issue may_issue prohibited ANY_WAIT BACKGROUND_CHECK DEALER_CHECK {
*jan, march, may, july, aug, oct, dec
if inlist(month,1,3,5,7,8,10,12) {
replace `var'=0 if `var'<=15
replace `var'=1 if `var'>15
}
*apr,june,sep,nov
else if inlist(month,4,6,9,11) {
replace `var'=0 if `var'<15
replace `var'=1 if `var'>=15
}
*feb
*leap years
else if month==2 & inlist(year,1992,1996,2000,2004,2008,2012) {
replace `var'=0 if `var'<=14
replace `var'=1 if `var'>14
}
*non leap years
else {
replace `var'=0 if `var'<14
replace `var'=1 if `var'>=14
}
}


*create new state variable with format consistent to that of NIBRS
gen STATE=.
replace STATE=1 if usps=="AL"
replace STATE=2 if usps=="AZ"
replace STATE=3 if usps=="AR"
replace STATE=5 if usps=="CO"
replace STATE=6 if usps=="CT"
replace STATE=7 if usps=="DE"
replace STATE=8 if usps=="DC"
replace STATE=10 if usps=="GA"
replace STATE=11 if usps=="ID"
replace STATE=12 if usps=="IL"
replace STATE=13 if usps=="IN"
replace STATE=14 if usps=="IA"
replace STATE=15 if usps=="KS"
replace STATE=16 if usps=="KY"
replace STATE=17 if usps=="LA"
replace STATE=18 if usps=="ME"
replace STATE=20 if usps=="MA"
replace STATE=21 if usps=="MI"
replace STATE=22 if usps=="MN"
replace STATE=23 if usps=="MS"
replace STATE=24 if usps=="MO"
replace STATE=25 if usps=="MT"
replace STATE=26 if usps=="NE"
replace STATE=28 if usps=="NH"
replace STATE=33 if usps=="ND"
replace STATE=34 if usps=="OH"
replace STATE=35 if usps=="OK"
replace STATE=36 if usps=="OR"
replace STATE=37 if usps=="PA"
replace STATE=38 if usps=="RI"
replace STATE=39 if usps=="SC"
replace STATE=40 if usps=="SD"
replace STATE=41 if usps=="TN"
replace STATE=42 if usps=="TX"
replace STATE=43 if usps=="UT"
replace STATE=44 if usps=="VT"
replace STATE=45 if usps=="VA"
replace STATE=46 if usps=="WA"
replace STATE=47 if usps=="WV"
replace STATE=48 if usps=="WI"

replace state = lower(state)
drop if missing(STATE)

merge m:1 state year using "Raw Data/Policy Data/annual_state_laws.dta"
keep if _merge != 2
drop _merge

*save
save "Processed Data/monthly_gun_policies.dta", replace







